{% extends 'admin/common/base.html' %}
{% block css %}
{{super()}}
<link href="{{url_for('admin.static',filename='ajax/libs/jquery-layout/jquery.layout-latest.css')}}" rel="stylesheet" />
<link href="{{url_for('admin.static',filename='ajax/libs/jquery-ztree/3.5/css/metro/zTreeStyle.css')}}"
    rel="stylesheet" />
{% endblock %}
{% block body %}

<body class="gray-bg">
    <div class="ui-layout-west">
        <div class="box box-main">
            <div class="box-header">
                <div class="box-title">
                    <i class="fa icon-grid"></i> 组织机构
                </div>
                <div class="box-tools pull-right">
                    <a type="button" class="btn btn-box-tool" href="#" onclick="dept()" title="管理部门"><i
                            class="fa fa-edit"></i></a>
                    <button type="button" class="btn btn-box-tool" id="btnExpand" title="展开" style="display:none;"><i
                            class="fa fa-chevron-up"></i></button>
                    <button type="button" class="btn btn-box-tool" id="btnCollapse" title="折叠"><i
                            class="fa fa-chevron-down"></i></button>
                    <button type="button" class="btn btn-box-tool" id="btnRefresh" title="刷新部门"><i
                            class="fa fa-refresh"></i></button>
                </div>
            </div>
            <div class="ui-layout-content">
                <div id="tree" class="ztree"></div>
            </div>
        </div>
    </div>
    <div class="ui-layout-center">
        <div class="container-div">
            <div class="row">
                <div class="col-sm-12 search-collapse">
                    <form id="user-form">
                        <input type="hidden" id="org_id" name="org_id">
                        <input type="hidden" id="parentId" name="parentId">
                        <div class="select-list">
                            <ul>
                                <li>
                                    登录名称：<input type="text" name="username" />
                                </li>
                                <li>
                                    手机号码：<input type="text" name="mobile" />
                                </li>
                                <li>
                                    用户状态：<select name="status">
                                        <option value="">所有</option>
                                        <option value="0">正常</option>
                                        <option value="1">停用</option>
                                    </select>
                                </li>
                                <li class="select-time">
                                    <label>创建时间： </label>
                                    <input type="text" class="time-input" id="startTime" placeholder="开始时间"
                                        name="stime" />
                                    <span>-</span>
                                    <input type="text" class="time-input" id="endTime" placeholder="结束时间"
                                        name="etime" />
                                </li>
                                <li>
                                    <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                            class="fa fa-search"></i>&nbsp;搜索</a>
                                    <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                            class="fa fa-refresh"></i>&nbsp;重置</a>
                                </li>
                            </ul>
                        </div>
                    </form>
                </div>

                <div class="btn-group-sm" id="toolbar" role="group">
                    <a class="btn btn-success {{check_perms('sys:user:add')}}" onclick="$.operate.addTab()">
                        <i class="fa fa-plus"></i> 新增
                    </a>
                    <a class="btn btn-primary single disabled {{check_perms('sys:user:edit')}}"
                        onclick="$.operate.editTab()">
                        <i class="fa fa-edit"></i> 修改
                    </a>
                    <!-- <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()">
                        <i class="fa fa-remove"></i> 删除
                    </a> -->
                    <!-- <a class="btn btn-info" onclick="$.table.importExcel()">
                        <i class="fa fa-upload"></i> 导入
                    </a>
                    <a class="btn btn-warning" onclick="$.table.exportExcel()">
                        <i class="fa fa-download"></i> 导出
                    </a> -->
                </div>

                <div class="col-sm-12 select-table table-striped">
                    <table id="bootstrap-table"></table>
                </div>
            </div>
        </div>
    </div>
</body>
{% endblock %}
{% block js %}
{{ super() }}
<script src="{{url_for('admin.static',filename='ajax/libs/jquery-layout/jquery.layout-latest.js')}}"></script>
<script src="{{url_for('admin.static',filename='ajax/libs/jquery-ztree/3.5/js/jquery.ztree.all-3.5.js')}}"></script>
<script>
    var ctx = '';
    var editFlag = "";
    var removeFlag = "";
    var resetPwdFlag = "";
    var prefix = ctx + "system/user";
    var editFlag = "{{check_perms('sys:user:edit')}}";
    var removeFlag = "{{check_perms('sys:user:remove')}}";
    var resetPwdFlag = "{{check_perms('sys:user:reset_pwd')}}";

    $(function () {
        var panehHidden = false;
        if ($(this).width() < 769) {
            panehHidden = true;
        }
        $('body').layout({ initClosed: panehHidden, west__size: 185 });
        // 回到顶部绑定
        if ($.fn.toTop !== undefined) {
            var opt = {
                win: $('.ui-layout-center'),
                doc: $('.ui-layout-center')
            };
            $('#scroll-up').toTop(opt);
        }
        queryUserList();
        queryDeptTree();
    });
    var sys_user_type_datas = {{ get_dict_data('sys_user_type') | tojson | safe}};
    function queryUserList() {
        var options = {
            url: "{{url_for('admin.sys.user.list')}}",
            createUrl: "{{url_for('admin.sys.user.add')}}",
            updateUrl: "{{url_for('admin.sys.user.edit')}}?id={id}",
            removeUrl: "{{url_for('admin.sys.user.remove')}}?id={id}",
            exportUrl: prefix + "/export",
            importUrl: prefix + "/importData",
            importTemplateUrl: prefix + "/importTemplate",
            sortName: "createTime",
            sortOrder: "desc",
            modalName: "用户",
            columns: [{
                checkbox: true
            },
            {
                field: 'id',
                title: '用户ID'
            },
            {
                field: 'username',
                title: '登录名称',
                sortable: true
            },
            {
                field: 'name',
                title: '用户名称'
            },
            {
                field: 'email',
                title: '邮箱',
                visible: false
            },
            {
                field: 'mobile',
                title: '手机'
            },
            {
                field: 'user_type',
                title: '用户类型',
                formatter: function (value, row, index) {
                    return $.table.selectDictLabel(sys_user_type_datas, value);
                }
            },
            {
                field: 'org.name',
                title: '部门'
            },
            {
                visible: editFlag == 'hidden' ? false : true,
                title: '用户状态',
                align: 'center',
                formatter: function (value, row, index) {
                    return statusTools(row);
                }
            },
            {
                field: 'vip_deadline',
                title: 'vip过期时间',
                sortable: true
            },
            {
                field: 'ctime',
                title: '创建时间',
                sortable: true
            },
            {
                title: '操作',
                align: 'center',
                formatter: function (value, row, index) {
                    var actions = [];
                    actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.editTab(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                    actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a> ');
                    actions.push("<a class='btn btn-default btn-xs " + resetPwdFlag + "' href='javascript:void(0)' onclick='resetPwd(" + row.id + ")'><i class='fa fa-key'></i>重置密码</a> ");
                    //var more = [];
                    //more.push("<a class='btn btn-default btn-xs " + resetPwdFlag + "' href='javascript:void(0)' onclick='resetPwd(" + row.id + ")'><i class='fa fa-key'></i>重置密码</a> ");
                    // more.push("<a class='btn btn-default btn-xs " + editFlag + "' href='javascript:void(0)' onclick='authRole(" + row.id + ")'><i class='fa fa-check-square-o'></i>分配角色</a>");
                    //actions.push('<a tabindex="0" class="btn btn-info btn-xs" role="button" data-container="body" data-placement="left" data-toggle="popover" data-html="true" data-trigger="hover" data-content="' + more.join('') + '"><i class="fa fa-chevron-circle-right"></i>更多操作</a>');
                    return actions.join('');
                }
            }]
        };
        $.table.init(options);
    }

    function queryDeptTree() {
        var url = "{{url_for('admin.sys.org.user_tree_data')}}";
        var options = {
            url: url,
            expandLevel: 1,
            onClick : zOnClick,
            data: {
                key: {
                    title: "title",
                    url: ''
                },
                simpleData: {
                    enable: true,
                    pIdKey: "parent_id",
                    
                }
            }
        };
        $.tree.init(options);

        function zOnClick(event, treeId, treeNode) {
            var treeId = treeNode.id;
            var treeName = treeNode.name;
            $("#org_id").val(treeId);
            $("#treeName").val(treeName);
            $.table.search();
        }
    }

    $('#btnExpand').click(function () {
        $._tree.expandAll(true);
        $(this).hide();
        $('#btnCollapse').show();
    });

    $('#btnCollapse').click(function () {
        $._tree.expandAll(false);
        $(this).hide();
        $('#btnExpand').show();
    });

    $('#btnRefresh').click(function () {
        queryDeptTree();
    });

    /* 用户管理-部门 */
    function dept() {
        var url = "{{url_for('admin.sys.org.index')}}";
        $.modal.openTab("部门管理", url);
    }

    /* 用户管理-重置密码 */
    function resetPwd(userId) {
        var url = "{{url_for('admin.sys.user.reset_pwd')}}?id=" + userId;
        $.modal.open("重置密码", url, '800', '300');
    }

    /* 用户管理-分配角色 */
    function authRole(userId) {
        var url = prefix + '/authRole/' + userId;
        $.modal.openTab("用户分配角色", url);
    }

    /* 用户状态显示 */
    function statusTools(row) {
        if (row.status == 1) {
            return '<i class=\"fa fa-toggle-off text-info fa-2x\" onclick="enable(\'' + row.id + '\')"></i> ';
        } else {
            return '<i class=\"fa fa-toggle-on text-info fa-2x\" onclick="disable(\'' + row.id + '\')"></i> ';
        }
    }

    /* 用户管理-停用 */
    function disable(userId) {
        $.modal.confirm("确认要停用用户吗？", function () {
            $.operate.post("{{url_for('admin.sys.user.change_status')}}", { "id": userId, "status": 1 });
        })
    }

    /* 用户管理启用 */
    function enable(userId) {
        $.modal.confirm("确认要启用用户吗？", function () {
            $.operate.post("{{url_for('admin.sys.user.change_status')}}", { "id": userId, "status": 0 });
        })
    }
</script>
{% endblock %}